<?php
class UserManageService
{
	/**
	 * 查找出全部未激活的用户
	 * @Author   Leikes
	 * @DateTime 2016-02-04T09:41:55+0800
	 * @return   Object                   全部未激活的用户
	 */
	public function findAll()
	{
		$users=User::where('is_activation','=',false)->get();
		return $users;
	}


	/**
	 * 用户id查询单个用户的信息
	 * @Author   Leikes
	 * @DateTime 2016-02-09T10:09:01+0800
	 * @param    array                   $data 用户id
	 * @return   message/userInfo                         异常信息或者用户信息
	 */
	public function findByUserInfo($data)
	{
		try
		{
			$this->verifyUserId($data);
			$userInfo=Userinfo::where('user_id','=',$data['userId'])->first();
			Session::put('userId',$data['userId']);
			return $userInfo;
		}
		catch(FieldValueAuthException $e)
		{
			return $e;
		}
	}
	
	/**
	 * 插入服务信息到数据库
	 * @Author   leikes
	 * @datetime 2016-02-15T09:48:22+0800
	 * @param    array                   $data 服务信息
	 * @return   exception                         [code,message]
	 */
	public function insertServerNumber($data)
	{
		try
		{
			DB::transaction(function() use($data)
			{
				$this->verifyServerInfo($data);
				$serverInfo=new Serverinfo;
				$serverInfo->register_number=$data['register_number'];
				$serverInfo->end_time=$data['end_time'];
				$serverInfo->is_use=false;
				$serverInfo->usergroup_id=Config::get('systemConfig.user_group_id.free_users_group');
				$serverInfo->save();
				$userId=Session::get('userId');
				$email=User::where('user_id','=',$userId)->first()->email;
				$this->sendMail($data,$email);
			});
			throw new NormalException;
		}
		catch(FieldValueAuthException $e)
		{
			return $e;
		}
		catch(NormalException $e)
		{
			return $e;
		}
		catch(SendEmailException $e)
		{
			return $e;
		}
		
	}

	/**
	 * 发送序列号邮件
	 * @Author   leikes
	 * @datetime 2016-02-15T09:50:07+0800
	 * @param    array                   $data  注册序列号信息
	 * @param    string                   $email 邮箱地址
	 * @throws   exception                          发送邮件错误异常
	 */
	private function sendMail($data,$email)
	{
		try
		{
			Mail::send('emails.activation.ActivationEMail', $data, function($message) use($data,$email)
			{	
					$message->to($email)->subject('恭喜你通过验证,返回激活码信息'); 
			});
		}
		catch(Exception $e)
		{
			throw new SendEmailException;
		}
	}

	/**
	 * 校验用户id是否正确
	 * @Author   Leikes
	 * @DateTime 2016-02-09T10:07:12+0800
	 * @param    array                   $data 用户id
	 * @throws   exception 						校验数据异常
	 */
	private function verifyUserId($data)
	{
		$rules=[
			'userId'=>'required|exists:users,user_id'
		];
		$validation = Validator::make($data,$rules);
		if( $validation->fails() )
		{
			throw new FieldValueAuthException( $validation->messages()->first() );
		}
	}

	/**
	 * 校验服务信息 
	 * @Author   leikes
	 * @datetime 2016-02-15T09:52:06+0800
	 * @param    array                   $data 服务信息
	 * @throws   exception                         检验异常
	 */
	private function verifyServerInfo($data)
	{
		$rules=[
			'register_number'=>'required|unique:serverinfos,register_number',
			'end_time'=>'required|after:'.date("Y/m/d")
		];
		$validation = Validator::make($data,$rules);
		if( $validation->fails() )
		{
			throw new FieldValueAuthException( $validation->messages()->first() );
		}
	}
}
?>